package evq.codec.core.managers.impl;

import evq.codec.core.entities.Dictionary;
import evq.codec.core.entities.Pgm;
import evq.codec.core.managers.VqMetricsManager;

public class DefaultVqMetricsManager implements VqMetricsManager {

	private static DefaultVqMetricsManager instance = new DefaultVqMetricsManager();
	
	private DefaultVqMetricsManager() {
		
	}
	
	public static DefaultVqMetricsManager getInstance() {
		return instance;
	}
	
	public double calculateCodificationRate(Dictionary dictionary) {
		return ((Math.log(dictionary.getSize()) / Math.log(2)) / dictionary.getQuantizatorDimension());
	}

	public double calculateMse(Pgm sourcePgm, Pgm quantizedPgm) {
		double mse = 0;
		for (int i = 0; i < sourcePgm.getImage().get(0).size(); i++) {
			for (int j = 0; j < sourcePgm.getImage().get(0).size(); j++) {
				mse += Math.pow((sourcePgm.getImage().get(i).get(j) - quantizedPgm.getImage().get(i).get(j)), 2);
			}
		}
		mse = mse/((sourcePgm.getHeight() * sourcePgm.getWidth()));
		return mse;
	}

	public double calculatePsnr(Pgm sourcePgm, Pgm quantizedPgm) {
		return 10 * Math.log10(Math.pow(sourcePgm.getGrayscale(), 2)/calculateMse(sourcePgm, quantizedPgm));
	}

}
